<?php

namespace app\api\controller;

use app\common\Common;
use app\common\Error;
use app\api\service\VideoService;
use think\facade\Request;

class Video extends Base
{
    private $service;

    public function __construct(VideoService $service)
    {
        $this->service = $service;
    }

    /**
     * 点赞和取消点赞
     * 
     * @dateTime 2022/09/03
     * @author zhixiang0215@126.com
     * @param int video_detail_id
     * @return fixed
     */
    public function getLike()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\Video::class)->scene('get_like')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->thumbUpOperation($params['video_detail_id'], $this->getUserID());

        if (!$result) {
            return Common::fail();
        }
        return Common::success();
    }

    /**
     * 分享
     */
    public function share()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\Video::class)->scene('share')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->share($params['video_detail_id'], $this->getUserID());

        if (!$result) {
            return Common::fail();
        }
        return Common::success();
    }

    /**
     * 查看付费电视扣K币
     * 
     * @dateTime 2022/09/05
     * @author zhixiang0215@126.com
     * @param int video_detail_id
     * @return \think\response\Json
     */
    public function watchVideoPay()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\Video::class)->scene('watch_video_pay')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->showPayForVideo($params, $this->getUserID());

        if ($result['code'] != 200) {
            return Common::fail($result['message'], $result['code']);
        }
        return Common::success($result['data']);
    }


    /**
     * 检查剧集是否需要充值
     * @return \think\response\Json
     */
    public function checkVideoPlay()
    {
        $params = Request::param();

        if (empty($params['video_detail_id'])) {
            return Common::fail('参数错误');
        }

        $uid = $this->getUserID();
        $result = $this->service->checkVideoPlay($params, $uid);
        $data = $result['data'] ?? [];
        if (!empty($data)) {
            $data['encryptStr'] = $this->service->encryptData($uid, $params, $data);
        }

        if ($result['code'] != 200) {
            return Common::fail($result['message'], $result['code'], $data);
        }
        return Common::success($data);
    }

    /**
     * 检查剧集是否需要观看广告
     * @return \think\response\Json
     */
    public function checkAdVideoPlay()
    {
        $params = Request::param();

        if (empty($params['video_detail_id'])) {
            return Common::fail('参数错误');
        }

        $result = $this->service->checkAdVideoPlay($params, $this->getUserID());

        if ($result['code'] != 200) {
            return Common::fail($result['message'], $result['code']);
        }
        return Common::success($result['data']);
    }

    /**
     * 观看广告播放短剧
     * @return \think\response\Json
     */
    public function watchAdVideoPlay()
    {
        $params = Request::param();

        if (empty($params['video_detail_id'])) {
            return Common::fail('参数错误');
        }

        $result = $this->service->addVideoAdPlayRecord($params, $this->getUserID());

        if ($result['code'] != 200) {
            return Common::fail($result['message'], $result['code']);
        }
        return Common::success($result['data']);
    }

    /**
     * 搜索剧名
     * 
     * @dateTime 2022/09/05
     * @author zhixiang0215@126.com
     * @param string name
     * @return json
     */
    public function search()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\Video::class)->scene('get_search_video')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }
        //by huangxiaoshan update 2023-07-24 支持按播放平台类型进行数据过滤.
        $data = $this->service->getSearchVideoData($params['name'], $this->getUserID(), ($params['play_platform'] ?? 1) ,($params['app_id'] ?? 1));
        return Common::success($data);
    }

    /**
     * 获取热门搜索数据
     * 
     * @dateTime 2022/09/05
     * @author zhixiang0215@126.com
     * @return json
     */
    public function getSearchList()
    {
        $params = Request::param();
        //by huangxiaoshan update 2023-07-24 支持按播放平台类型进行数据过滤.
        $data = $this->service->getHotSearchData($this->getUserID(),$params);
        return Common::success($data);
    }

    /**
     * 获取推荐页数据列表
     * 
     * @dateTime 2022/08/10
     * @author zhixiang0215@126.com
     * @return json
     */
    public function getRecommendList()
    {
        $params = Request::param();
        $data = $this->service->getRecommendVideoData($params, $this->getUserID());
        return Common::success($data);
    }

    // 获取短剧信息及视频
    public function info()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\Video::class)->scene('info')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getVideoInfo($params['video_id']);
        return Common::success($data);
    }

    // 获取指定短剧剧集列表
    public function list()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\Video::class)->scene('list')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getVideoList($params['video_id']);
        return Common::success($data);
    }

    // 播放完毕后跳转的短剧
    public function otherVideo()
    {
         $params = Request::param();
        // try {
        //     validate(\app\api\validate\Video::class)->scene('other_video')->check($params);
        // } catch (\Exception $e) {
        //     return Common::fail($e->getError());
        // }

        $data = $this->service->getOtherVideoList($this->getUserID(),$params );
        return Common::success($data);
    }

    //最近观看
    public function recentlyWatched()
    {
        $data = $this->service->getRecentlyWatchVideo($this->getUserID());
        if ($data) {
            return Common::success($data);
        }
        return Common::fail();
    }
}